﻿@using Huirui.Cavan.Domain.MainBoundedContext.SystemModule
@model Huirui.Cavan.Presentation.ViewModel.SystemModule.IEmployeeViewModel
@{Layout = null;}
@using (Html.BeginForm(ActionConst.ADD, ControllerConst.EMPLOYEE, FormMethod.Post))
{
    <div class="tool_bar">
        <input id="emp_save" type="submit" style="display: none;" />
        <input id="emp_reset" type="reset" style="display: none" />
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-save'" onclick="javascript:$('#emp_save').click()">保存</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-copy'">复制</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-undo'" id="employeeReset">重置</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-collapse'" onclick="toggleHeard()">折叠/展开</a>
    </div>
    <table class="form_heard">
        <tr>
            <td class="c100">
                <label class="BlueRequired">编号:</label>
            </td>
            <td class="c160">
                @{
    var createTypeList = new List<SelectListItem>();
    createTypeList.Add(new SelectListItem() { Selected = true, Text = "自动生成", Value = "1" });
    createTypeList.Add(new SelectListItem() { Text = "手工输入", Value = "0" });
                }
                <table class="form_heard" style="border-collapse: collapse">
                    <tr>
                        <td class="c80" style="padding: 0">
                            @Html.DropDownList("employeeIdCreateType", createTypeList, new { width = "50" })
                        </td>
                        <td class="c80">
                            @Html.TextBoxFor(m => m.NoPrefixId, new { id = "employee_NoPrefixId" })
                            @Html.TextBoxFor(m => m.Item.Id, new { id = "employee_Id",style = "display:none;"})
                             
                        </td>
                    </tr>
                </table>
            </td>
            <td class="c100">
                <label class="BlueRequired">姓名:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.Name, new { id = "employee_name", required = "true", validType = "minLength[2]", @class = "easyui-validatebox" })
            </td>
            <td class="c10"></td>
            <td class="c100">
                <label>性别:</label>
            </td>
            <td class="c160">
                @Html.DropDownListFor(m => m.Item.Sex, typeof(SystemIdentifier.Sex), new { id = "employee_sex", })
            </td>
            <td class="c100">
                <label class="BlueRequired">部门:</label>
            </td>
            <td class="c160">
                @Html.DropDownListFor(m => m.Item.Department.Id, Model.DepartmentList, "请选择部门", new { id = "department_type" })
            </td>
        </tr>
        <tr>
            <td class="c100">
                <label class="BlueRequired">岗位:</label>
            </td>
            <td class="c160">
                @Html.DropDownListFor(m => m.Item.Job.Id, Model.Jobs, "请选择岗位", new { id = "employee_job" })
            </td>
            <td class="c100">
                <label>佣金率:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.CommissionRate, new { id = "employee_commissionRate" })
            </td>
            <td class="c10">
                @Html.Raw("%")
            </td>
            <td class="c100">
                <label>生日:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.Birthday, new { id = "employee_birthday", @class = "Wdate", @Value = (Model.Item.IsNotNull() && Model.Item.Birthday.HasValue) ? Model.Item.Birthday.Value.ToString("yyyy-MM-dd") : DateTime.Now.ToString("yyyy-MM-dd"), @type = "text", @onfocus = "WdatePicker({wdate:true,isShowClear:false,readOnly:true,skin:'whyGreen',maxDate:'%y-%M-%d'})" })
            </td>
            <td class="c100">
                <label>手机:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.Mobile, new { id = "employee_mobile" })
            </td>
        </tr>
        <tr>
            <td class="c100">
                <label>E-Mail:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.Email, new { id = "employee_email" })
            </td>
            <td class="c100">
                <label>备注:</label>
            </td>
            <td colspan="6">
                @Html.TextBoxFor(m => m.Item.Remark, new { id = "employee_remark" })
            </td>
        </tr>
    </table>
}
<table id="employee_dg">
    <thead>
        <tr>
            <th data-options="field:'ck',checkbox:'true',width:10"></th>
            <th data-options="field:'NoPrefixId',width:80">编号</th>
            <th data-options="field:'Name',width:80">姓名</th>
            <th data-options="field:'DepartmentName',width:80">部门</th>
            <th data-options="field:'EmployeJob',width:80">岗位</th>
            <th data-options="field:'CommissionRate',width:40">佣金率(%)</th>
            <th data-options="field:'Sex',width:20">性别</th>
            <th data-options="field:'SexId',hidden:'true',width:0"></th>
            <th data-options="field:'Birthday',width:80">生日</th>
            <th data-options="field:'Email',width:100">E-Mail</th>
            <th data-options="field:'Mobile',width:100">手机号码</th>
            <th data-options="field:'SMSQueryEnabled',width:40">短信状态</th>
        </tr>
    </thead>
</table>
<div id="employeetb">
    <a href="#" class="easyui-linkbutton" iconcls="icon-remove" plain="true" id="deleteEmployee">选择删除</a>
    <a href="#" class="easyui-linkbutton" iconcls="icon-print" plain="true" onclick="javascript:alert('打印列表')">打印列表</a>
    <a href="#" class="easyui-linkbutton" iconcls="icon-excel" plain="true" onclick="javascript:alert('引出')">引出</a>
</div>
@{ Html.RenderAction(ActionConst.ADD, ControllerConst.JOB); }
<script type="text/javascript">
    $(function ()
    {
        $("#employee_NoPrefixId").attr("disabled", true);

        $("#form[action^='/employee']").form({
            onSubmit: function () {
                return $(this).form('validate');
            }
        });
        $("form[action^='/employee']").submit(function () {
            $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                success: function (data) {
                    if (data.IsSuccess) {
                        $('#employee_dg').datagrid('reload');
                    }
                },
                error: function () {
                    alert('系统繁忙，请稍候再试');
                }
            })

            return false;
        });

        $('#employee_dg').datagrid({
            class: 'easyui-datagrid',
            title: '职员列表',
            idfield: 'ID',
            pagination: 'true',
            toolbar: '#employeetb',
            singleSelect: 'true',
            selectOnCheck: 'true',
            fitColumns: 'true',
            url: '/employee/list',
            onSelect: function (rowIndex, rowData) {
                $("#employee_NoPrefixId").val(rowData.NoPrefixId).attr("readonly", true);
                $("#employee_Id").val(rowData.Id);
                $("#employee_name").val(rowData.Name);
                $("select#department_type").attr('value', rowData.DepartmentId);
                $("select#employee_job").attr('value', rowData.EmployeJobId);
                $("#employee_commissionRate").val(rowData.CommissionRate);
                $("select#employee_sex").attr('value', rowData.SexId);
                $("#employee_birthday").val(rowData.Birthday);
                $("#employee_email").val(rowData.Email);
                $("#employee_mobile").val(rowData.Mobile);
                $("#employee_remark").val(rowData.Remark);
            }
        });

        $("select#employee_job").bind("change", function () {
            if ($(this).val() == "-1") {
                openWindow("addJob");
            }
        });

        $("select#employeeIdCreateType").bind("change", function () {
            if ($(this).val() == "1") {
                $("#employee_NoPrefixId").attr("disabled", true);
            }
            else {
                $("#employee_NoPrefixId").attr("disabled", false);
            }
        });

        $("#employeeReset").bind("click", function ()
        {
            $('#emp_reset').click();
            if ($('select#employeeIdCreateType').val() == "1")
            {
                $("#employee_NoPrefixId").attr("disabled", true);
            }
            else
            {
                $("#employee_NoPrefixId").attr("disabled", false);
            }
        });

        $("form[action^='/job/add']").submit(function () {
            $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                success: function (data) {
                    if (data) {
                        $("select#employee_job option").each(function () {
                            if ($(this).val() == "-1") {
                                $(this).remove();
                            }
                        });
                        $("<option value='" + data.Messages[1] + "'>" + data.Messages[2] + "</option>").appendTo($("select#employee_job"));
                        $("<option value='-1'>添加...</option>").appendTo($("select#employee_job"));
                        $("select#employee_job").attr("value", data.Messages[1]);
                        closeWindow("addJob");
                    }
                },
                error: function () {
                    alert('系统繁忙，请稍候再试');
                }
            })

            return false;
        });



        $("#deleteEmployee").bind("click", function () {
            if ($('#employee_dg').datagrid('getChecked').length) {
                var selected = $('#employee_dg').datagrid('getChecked');

                var effectRow = new Object();
                if (selected.length) {
                    effectRow["selected"] = JSON.stringify(selected);
                }

                $.post("/employee/delete", effectRow, function (data) {
                    if (data.IsSuccess) {
                        $('#employee_dg').datagrid('reload');
                        $('#employee_dg').datagrid('clearChecked');
                    }
                }, "JSON").error(function () {
                    $.messager.alert("提示", "网络异常，请稍候再试");
                });
            }
        });
    });
</script>
